package com.zhq.wx.controller;

import com.zhq.bean.BootStrapTableExpress;
import com.zhq.bean.Express;
import com.zhq.bean.Message;
import com.zhq.bean.User;
import com.zhq.mvc.ResponseBody;
import com.zhq.service.ExpressService;
import com.zhq.util.DateFormatUtil;
import com.zhq.util.JSONUtil;
import com.zhq.util.UserUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

/**
 * @Author:钟豪强
 * @Description:
 * @Date Created in  2020-08-19 22:53
 * @Modified By:
 */
public class ExpressController {

    @ResponseBody("/wx/findExpressByUserPhone.do")
    public String findByUserPhone(HttpServletRequest request, HttpServletResponse response){
        User wxUser = UserUtil.getWxUser(request.getSession());
        String userPhone = wxUser.getUserPhone();
        List<Express> list = ExpressService.findByUserPhone(userPhone);
        List<BootStrapTableExpress> list2 = new ArrayList<>();
        for (Express e:list) {
            String inTime = DateFormatUtil.format(e.getInTime());
            String outTime = e.getOutTime() == null ? " " : DateFormatUtil.format(e.getOutTime());
            String status = e.getStatus() == 0 ? "未取件":"已取件";
            String code = e.getCode() == null ? "已取件" : e.getCode();
            BootStrapTableExpress e2 = new BootStrapTableExpress(e.getId(),e.getNumber(),e.getUsername(),e.getUserPhone(),e.getCompany(),code,inTime,outTime,status,e.getSysPhone());
            list2.add(e2);
        }
        Message msg = new Message();
        if (list2.size() == 0) {
            msg.setStatus(-1);
        }else{
            msg.setStatus(0);
            //用流的方式处理过滤数据
            Stream<BootStrapTableExpress> status0Express = list2.stream().filter(express -> {
                if (express.getStatus().equals("未取件")) {
                    //已取件的过滤掉
                    return true;
                } else {
                    return false;
                }
            }).sorted((o1,o2) -> {
                long o1time = DateFormatUtil.toTime(o1.getInTime());
                long o2time = DateFormatUtil.toTime(o2.getInTime());
                return (int) (o1time-o2time);
            });

            Stream<BootStrapTableExpress> status1Express = list2.stream().filter(express -> {
                if (express.getStatus().equals("已取件")) {
                    //未取件的过滤掉
                    return true;
                } else {
                    return false;
                }
            }).sorted((o1,o2) -> {
                long o1time = DateFormatUtil.toTime(o1.getInTime());
                long o2time = DateFormatUtil.toTime(o2.getInTime());
                return (int) (o1time-o2time);
            });
            Object[] s0 = status0Express.toArray();
            Object[] s1 = status1Express.toArray();
            Map data = new HashMap();
            data.put("status0",s0);
            data.put("status1",s1);
            msg.setData(data);
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/wx/userExpressList.do")
    public String expressList(HttpServletRequest request, HttpServletResponse response){
        System.out.println("请收到请求");
        String userPhone = request.getParameter("userPhone");
        System.out.println(userPhone);
        List<Express> list = ExpressService.findByUserPhoneAndStatus(userPhone, 0);
        List<BootStrapTableExpress> list2 = new ArrayList<>();
        for (Express e:list) {
            String inTime = DateFormatUtil.format(e.getInTime());
            String outTime = e.getOutTime() == null ? " " : DateFormatUtil.format(e.getOutTime());
            String status = e.getStatus() == 0 ? "未取件":"已取件";
            String code = e.getCode() == null ? "已取件" : e.getCode();
            BootStrapTableExpress e2 = new BootStrapTableExpress(e.getId(),e.getNumber(),e.getUsername(),e.getUserPhone(),e.getCompany(),code,inTime,outTime,status,e.getSysPhone());
            list2.add(e2);
        }
        Message msg = new Message();
        if (list2.size() == 0) {
            msg.setStatus(-1);
            msg.setResult("未查询到快递");
        }else{
            msg.setStatus(0);
            msg.setResult("查询成功");
            msg.setData(list2);
        }
        return JSONUtil.toJSON(msg);
    }

    @ResponseBody("/ul/indexQueryExpress.do")
    public String findExpressByNumber(HttpServletRequest request, HttpServletResponse response){
        System.out.println("收到请求");
        String number = request.getParameter("expressNum");
        Express e = ExpressService.findByNumber(number);
        String inTime = DateFormatUtil.format(e.getInTime());
        String outTime = e.getOutTime() == null ? " " : DateFormatUtil.format(e.getOutTime());
        String status = e.getStatus() == 0 ? "未取件":"已取件";
        String code = e.getCode() == null ? "已取件" : e.getCode();
        BootStrapTableExpress e2 = new BootStrapTableExpress(e.getId(),e.getNumber(),e.getUsername(),e.getUserPhone(),e.getCompany(),code,inTime,outTime,status,e.getSysPhone());
        Message msg = new Message();
        if (e != null) {
            msg.setStatus(0);
            msg.setResult(e2.toString());
        }else{
            msg.setStatus(-1);
            msg.setResult("没有该快递单号的快递");
        }
        return JSONUtil.toJSON(msg);
    }
}
